﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class YCAFSAV95Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();

        #endregion
        public YCAFSAV95Report()
            : base()
        {
        }
        public YCAFSAV95Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }


        }
        #region------------------------------Function------------------------
        /*VKPPOSTYP	TYPE	VKPNEUDAT	CUSTOMER	CUNAME	VKPAUFKRS	ORDERNO	ITEM	DESCR	
       ARTGRP	ARTNAME	VKPWUTERM	VKPLIEFTERM	VKPVERZTERM1	VKPVERZTERM2	VKPVERZTERM3	
                    VKPVERZTERM4	STAFF	SACHB	STATUS	VKPERL	REMARK*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["ICC_ABPOS_MAT"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("ABID", typeof(System.String));
                dtOutput.Columns.Add("ABKKDAUFTNR", typeof(System.String));
                dtOutput.Columns.Add("ABEND", typeof(System.String));
                dtOutput.Columns.Add("ABPSPGRD", typeof(System.String));
                dtOutput.Columns.Add("ABPGESSTA", typeof(System.String));
                dtOutput.Columns.Add("QC", typeof(System.String));
                dtOutput.Columns.Add("KRS", typeof(System.String));
                dtOutput.Columns.Add("LFD", typeof(System.String));
                dtOutput.Columns.Add("PROGRP", typeof(System.String));
                dtOutput.Columns.Add("PROGRPD", typeof(System.String));
                dtOutput.Columns.Add("SECTION", typeof(System.String));
                /*dtOutput.Columns.Add("ARTGRP", typeof(System.String));
                dtOutput.Columns.Add("ARTGRPD", typeof(System.String));
                dtOutput.Columns.Add("MATART", typeof(System.String));
                dtOutput.Columns.Add("MATARTD", typeof(System.String));*/
                dtOutput.Columns.Add("EXTFTYNAME", typeof(System.String));        
                dtOutput.Columns.Add("ABPART", typeof(System.String));
                dtOutput.Columns.Add("ARTDES", typeof(System.String));
                dtOutput.Columns.Add("PHOTOLINK", typeof(System.String));
                dtOutput.Columns.Add("ABPVAR", typeof(System.String));
                dtOutput.Columns.Add("ABPFARBE", typeof(System.String));
                dtOutput.Columns.Add("CDES", typeof(System.String));
                dtOutput.Columns.Add("MPRICE", typeof(System.String));
                dtOutput.Columns.Add("ARWERT", typeof(System.String));
                        
                dtOutput.Columns.Add("MAINFABRIC", typeof(System.String));
                dtOutput.Columns.Add("FSFINISHING", typeof(System.String));
                dtOutput.Columns.Add("SPREMARK", typeof(System.String));
                dtOutput.Columns.Add("TQTY", typeof(System.String));
                dtOutput.Columns.Add("DQTY", typeof(System.String));
                
               /* dtOutput.Columns.Add("RSSENDDAT", typeof(System.String));
                dtOutput.Columns.Add("RSGETDATE", typeof(System.String));
                dtOutput.Columns.Add("RSACSENDDATE", typeof(System.String));
                dtOutput.Columns.Add("RSACGETDATE", typeof(System.String));
                dtOutput.Columns.Add("RSACCDATE", typeof(System.String));
                dtOutput.Columns.Add("RSACCCOMMENTS", typeof(System.String));

                dtOutput.Columns.Add("RSSSTAMPSENDATE", typeof(System.String));*/
                dtOutput.Columns.Add("EMFSENDDAT", typeof(System.String));
                dtOutput.Columns.Add("EAFSENDDAT", typeof(System.String));
                dtOutput.Columns.Add("EACSENDATE", typeof(System.String));
                dtOutput.Columns.Add("ELABELDATE", typeof(System.String));

                dtOutput.Columns.Add("EREMARK", typeof(System.String));
                dtOutput.Columns.Add("ECHECKDAT", typeof(System.String));
                dtOutput.Columns.Add("EFCHECKDAT", typeof(System.String));
                dtOutput.Columns.Add("PRSAMPLEAPP", typeof(System.String));
                
                dtOutput.Columns.Add("TQTYD", typeof(System.String));
                dtOutput.Columns.Add("TQTYP", typeof(System.String));
                dtOutput.Columns.Add("PCQTY", typeof(System.String));
                dtOutput.Columns.Add("PCDQTY", typeof(System.String));
                dtOutput.Columns.Add("PCRATIO", typeof(System.String));

                dtOutput.Columns.Add("PPQTY", typeof(System.String));
                dtOutput.Columns.Add("PDQTY", typeof(System.String));

                dtOutput.Columns.Add("PCDAT", typeof(System.String));
                dtOutput.Columns.Add("PSDAT", typeof(System.String));
                dtOutput.Columns.Add("PSLAB", typeof(System.String));
                dtOutput.Columns.Add("PMOUT", typeof(System.String));

                dtOutput.Columns.Add("EPRODDATE", typeof(System.String));
                
                
                dtOutput.Columns.Add("PPENDAT", typeof(System.String));
                dtOutput.Columns.Add("PAENDAE", typeof(System.String));
                dtOutput.Columns.Add("PSTARTDATE", typeof(System.String));
                dtOutput.Columns.Add("PAENDSTOPDAT", typeof(System.String));
                dtOutput.Columns.Add("OTDRATIO", typeof(System.String));


                dtOutput.Columns.Add("PQCINDAT", typeof(System.String));
                dtOutput.Columns.Add("PQCINREMARK", typeof(System.String));
                dtOutput.Columns.Add("PQCMIDDAT", typeof(System.String));
                dtOutput.Columns.Add("PQCMIDREMARK", typeof(System.String));
                
                dtOutput.Columns.Add("PQCDAT", typeof(System.String));
                dtOutput.Columns.Add("PQC2QTY", typeof(System.String));
                dtOutput.Columns.Add("PQC2REMARK", typeof(System.String));
                
                
                dtOutput.Columns.Add("PREPRTNDAT", typeof(System.String));
                dtOutput.Columns.Add("PSAMRTDAT", typeof(System.String));
                dtOutput.Columns.Add("PQADAE", typeof(System.String));

                dtOutput.Columns.Add("PQAISSUE", typeof(System.String));
                dtOutput.Columns.Add("PQAREMARK", typeof(System.String));
                dtOutput.Columns.Add("UM", typeof(System.String));
                dtOutput.Columns.Add("RATIO", typeof(System.String));
                dtOutput.Columns.Add("S", typeof(System.String));
                dtOutput.Columns.Add("M", typeof(System.String));
                dtOutput.Columns.Add("L", typeof(System.String));
                dtOutput.Columns.Add("XL", typeof(System.String));
                dtOutput.Columns.Add("SP", typeof(System.String));
                dtOutput.Columns.Add("MP", typeof(System.String));
                dtOutput.Columns.Add("LP", typeof(System.String));
                dtOutput.Columns.Add("XLP", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                foreach (DataRow rx in dtOutput.Rows)
                { 
                    //rx["ARTGRPD"] = GetGB2312Value(rx["ARTGRPD"].ToString().Trim());
                    //rx["MATARTD"] = GetGB2312Value(rx["MATARTD"].ToString().Trim());
                    rx["PROGRPD"] = GetGB2312Value(rx["PROGRPD"].ToString().Trim());
                    rx["ARTDES"] =GetGB2312Value(rx["ARTDES"].ToString().Trim());
                    rx["CDES"] = GetGB2312Value(rx["CDES"].ToString().Trim());
                    rx["MPRICE"] = Convert.ToDecimal((rx["MPRICE"].ToString()))/1000;
                    rx["ARWERT"] = Convert.ToDecimal((rx["ARWERT"].ToString()))/1000;
                    rx["EXTFTYNAME"] = GetGB2312Value(rx["EXTFTYNAME"].ToString().Trim());                    
                    rx["MAINFABRIC"] = GetGB2312Value(rx["MAINFABRIC"].ToString().Trim());
                    //rx["RSACCCOMMENTS"] = GetGB2312Value(rx["RSACCCOMMENTS"].ToString().Trim());
                    rx["EREMARK"] = GetGB2312Value(rx["EREMARK"].ToString().Trim());
                    rx["PQC2REMARK"] = GetGB2312Value(rx["PQC2REMARK"].ToString().Trim());
                    rx["PQAISSUE"] = GetGB2312Value(rx["PQAISSUE"].ToString().Trim());
                    rx["PQAREMARK"] = GetGB2312Value(rx["PQAREMARK"].ToString().Trim());
                    rx["PPQTY"] = rx["TQTY"];
                    rx["PDQTY"] = Convert.ToDecimal((rx["PCQTY"].ToString())) - Convert.ToDecimal((rx["TQTY"].ToString()));                    
                }
               
                exAgent.DataTableToExcel(dtOutput, xStart, 1); 
                x = xStart - 1;
                foreach (DataRow rx in dtOutput.Rows)
                {
                    x++; y = 17;
                    string str = rx["PHOTOLINK"].ToString();
                    EventDispatcher.Instance.RaiseStatus(str, StatusEventType.RunningProgress);
                    if (System.IO.File.Exists(str))
                    {
                        str = str.Replace("/", "\\");
                        exAgent.SetCellsPicture(x, y, str,40,60);
                    } 
                }
                //int count = dtOutput.Rows.Count;
                //exAgent.SetCellValue(1, 1, 1);
                //exAgent.BatchConvert2Number(3, 12, count + 2, 12);
                //exAgent.BatchConvert2Number(3, 19, count + 2, 19);
                //exAgent.BatchConvert2Number(3, 31, count + 2, 31);
                //exAgent.SetCellValue(1, 1, "计品部订单统计");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "YCA";
                strExcelFile = @"YCA\YCAFSAV95.xls";
                Standard = "FSAV95";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\YCA\YCAFSAV95.xls");
                destfolder = SetFolder(Root, "生产进度");
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = @"生产进度" + filedate;

                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                FillInExcelData(dsSource, 4, 1);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}